import 'package:flutter/material.dart';
//import 'package:flutter_braintree/flutter_braintree.dart';

class PayPalPaymentPage extends StatefulWidget {
  final String tokenizationKey;
  final String initialAmount;

  const PayPalPaymentPage({
    Key? key,
    required this.tokenizationKey,
    required this.initialAmount,
  }) : super(key: key);

  @override
  // ignore: library_private_types_in_public_api
  _PayPalPaymentPageState createState() => _PayPalPaymentPageState();
}

class _PayPalPaymentPageState extends State<PayPalPaymentPage> {
  String _statusMessage = "Ready to pay"; // 支付状态信息
  bool _isProcessing = false; // 支付处理中状态
  String _amount = ""; // 支付金额

  @override
  void initState() {
    super.initState();
    _amount = widget.initialAmount; // 初始化支付金额
  }

  // // 发起支付
  Future<void> initiatePayment() async {
    setState(() {
      _isProcessing = true;
      _statusMessage = "Processing payment...";
    });

    // try {
    //   var request = BraintreeDropInRequest(
    //     //Ad3Rhi-2-PDng-m4RJ5HllObSpftKcZ22Solr6wvEG-k3_EZsddES02nPynfvDYbYf_HfeJwLXIPWqZF
    //     tokenizationKey: widget.tokenizationKey,
    //     collectDeviceData: true,
    //     paypalRequest: BraintreePayPalRequest(
    //       amount: _amount,
    //       currencyCode: "USD",
    //       displayName: "Your App Name",
    //     ),
    //     cardEnabled: false, // 如果需要支持信用卡支付，保留此项
    //   );

    //   BraintreeDropInResult? result = await BraintreeDropIn.start(request);

    //   if (result != null) {
    //     setState(() {
    //       _statusMessage =
    //           "Payment Successful: ${result.paymentMethodNonce.nonce}";
    //       _isProcessing = false;
    //     });
    //     // ignore: use_build_context_synchronously
    //     Navigator.pop(context, result.paymentMethodNonce.nonce); // 返回支付结果
    //   } else {
    //     setState(() {
    //       _statusMessage = "Payment Cancelled";
    //       _isProcessing = false;
    //     });
    //   }
    // } catch (e) {
    //   setState(() {
    //     _statusMessage = "Payment Failed: ${e.toString()}";
    //     _isProcessing = false;
    //   });
    // }
  }

  // 更新支付金额
  void updateAmount(String newAmount) {
    // setState(() {
    //   _amount = newAmount;
    //   _statusMessage = "Amount updated to $newAmount";
    // });
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: const Text("Pay with PayPal"),
      ),
      body: Padding(
        padding: const EdgeInsets.all(16.0),
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: [
            Text(
              "Payment Amount: $_amount USD",
              style: const TextStyle(fontSize: 18),
            ),
            const SizedBox(height: 20),
            TextField(
              decoration: const InputDecoration(
                labelText: "Enter new amount",
                border: OutlineInputBorder(),
              ),
              keyboardType: TextInputType.number,
              onSubmitted: updateAmount, // 更新支付金额
            ),
            const SizedBox(height: 20),
            _isProcessing
                ? const CircularProgressIndicator()
                : ElevatedButton(
                    onPressed: initiatePayment,
                    child: const Text("Pay with PayPal"),
                  ),
            const SizedBox(height: 20),
            Text(
              _statusMessage,
              style: const TextStyle(fontSize: 16, color: Colors.grey),
              textAlign: TextAlign.center,
            ),
          ],
        ),
      ),
    );
  }
}
